//
//  PictureView.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/7.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

protocol PictureViewDelegate: NSObjectProtocol {
    
    func pictureView(pictureView:PictureView,didSelectItem:UIImageView,index:Int);
    
}

class PictureView: UIView {
    
    @IBOutlet weak var viewH: NSLayoutConstraint!
    
    weak var delegate:PictureViewDelegate?
    
    private var blog:Blog?
  
    private let perline = 3
    private let innerMargin = 16
    private let itemMargin = 8
    private var width:Int!
    
    func setBlog(_ blog: Blog) {
        
        
        if let lastBlog = self.blog {
            
            if lastBlog == blog {
                
                return
            }
        }
        
        self.blog = blog
        calcCellHeight()
        setupData()
    }
    
    override func awakeFromNib() {
        
        super.awakeFromNib()
        
        setupUI()
    }
    
    private func calcCellHeight() {
        
        var count = blog?.pictures_new?.count ?? 0
        
        if let videocount = blog?.videos?.count {
            
            if videocount > 0 {
                
                count = videocount
            }
        }
        
        if count == 0 {
            self.viewH.constant = 0
            return
        }

        let lines = ceil(Double(count) / Double(perline))
        
        self.viewH.constant = CGFloat(lines * Double(width) + Double(itemMargin) * (lines - 1))
    }
    
    
    private func setupUI() {
        
        self.clipsToBounds = true
        
        width = (kScreenWidth - (innerMargin * 2) - itemMargin * (perline - 1)) / perline
        
        let rect = CGRect(x: 0, y: 0, width: width, height: width)
        
        for i in 0..<9 {
            
            let row = i / perline
            let col = i % perline
            let iv = UIImageView(frame: rect)
            
            iv.contentMode = .scaleAspectFill
            iv.layer.borderWidth = 1.0
            iv.layer.borderColor = UIColor.black.withAlphaComponent(0.1).cgColor
            iv.clipsToBounds = true
            iv.layer.cornerRadius = 4
            
            let xOffset = col * (width + itemMargin)
            
            let yOffset = row * (width + itemMargin)
            
            iv.frame = rect.offsetBy(dx: CGFloat(xOffset), dy: CGFloat(yOffset))
            
            addSubview(iv)
            
            let videoIV = UIImageView(image: #imageLiteral(resourceName: "video"))
            
            videoIV.bounds = CGRect(x: 0, y: 0, width: 30, height: 30)
            videoIV.center = CGPoint(x: CGFloat(width) / 2.0,y: CGFloat(width) / 2.0)
            
            videoIV.tag = 111
            
            iv.addSubview(videoIV)
            videoIV.isHidden = true
            
            iv.tag = i
            
            iv.isUserInteractionEnabled = true
            iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectItem)))
        }
    }
    
    private func setupData() {
        
        for view in subviews {
            
            view.isHidden = true
        }
        
        
        var count = blog?.pictures_new?.count ?? 0
        
        if let videocount = blog?.videos?.count {
            
            if videocount > 0 {
                
                count = videocount
            }
        }
        
        if count == 0 {
            self.viewH.constant = 0
            return
        }
        
        
        for index in 0..<count {
            
            guard let iv = subviews[index] as? UIImageView else {
                
                return
            }
            iv.isHidden = false
            
            let videoIV = iv.viewWithTag(111) as! UIImageView
            
            if let videocount = blog?.videos?.count {
                
                if videocount > index {
                    
                    let video = blog?.videos?[index]

                    iv.setImage(string: video?.video_cover, placeholder: #imageLiteral(resourceName: "placeholder"))
                    
                    videoIV.isHidden = false
                    
                    continue
                }
            }
            
            iv.setImage(string: blog?.pictures_new?[index].getQiniuThumb(thumbImageWidth), placeholder: #imageLiteral(resourceName: "placeholder"))
            videoIV.isHidden = true
        }
        
    }
    
    @objc private func selectItem(_ gesture:UITapGestureRecognizer) {
        
        
        guard let iv = gesture.view as? UIImageView else { return }
        
        delegate?.pictureView(pictureView: self, didSelectItem: iv, index: iv.tag)
        
    }
    
}

